<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-row class="search-input">
          <el-form-item label="账期名称：" prop="peroid_name">
            <el-input v-model="filters.peroid_name" placeholder="账期名称"></el-input>
          </el-form-item>
          <el-form-item label="收付账期：" prop="payment_peroid">
            <el-select v-model="filters.payment_peroid" placeholder="收付账期">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in paymentPeroidOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账期类型：" prop="peroid_type">
            <el-select v-model="filters.peroid_type" placeholder="账期类型">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in peroidTypeOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生效日：" prop="startTime">
            <el-date-picker
              v-model="filters.startTime"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="生效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="失效日" prop="endTime">
            <el-date-picker
              v-model="filters.endTime"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="失效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar">
      <el-form :inline="true" :size="size">
        <el-form-item class="w100 t_r">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :width="1000"
      :height="250"
      :data="pageResult"
      :columns="filterColumns"
      :showDetailOperation="true"
      :showEditOperation="true"
      :showBatchDelete="true"
      :showDeleteOperation="true"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @handleDetail="handleDetail"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="140px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
          <el-form-item label="ID：" prop="id" v-if="false">
            <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="账期名称：" prop="peroid_name" class="serarch-inner">
            <el-input v-model="dataForm.peroid_name" auto-complete="off" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="收付账期：" prop="payment_peroid">
            <el-select v-model="dataForm.payment_peroid" placeholder="收付账期">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in paymentPeroidOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计算基准日期：" prop="benchmark_type">
            <el-select v-model="dataForm.benchmark_type" placeholder="计算基准日期">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in benchmarkTypeOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生效日：" prop="effective_date">
            <el-date-picker
              v-model="dataForm.effective_date"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="生效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="失效日：" prop="fail_date">
            <el-date-picker
              v-model="dataForm.fail_date"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="失效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="账期类型：" prop="peroid_type">
            <el-select v-model="dataForm.peroid_type" placeholder="账期类型">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in peroidTypeOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
    <!--详情界面-->
    <el-dialog
      :title="'账期设置详情'"
      width="40%"
      :visible.sync="detailDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="140px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
          <el-form-item label="ID：" prop="id" v-if="false">
            <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="账期名称：" prop="peroid_name" class="serarch-inner">
            <el-input v-model="dataForm.peroid_name" auto-complete="off" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="收付账期：" prop="payment_peroid">
            <el-select v-model="dataForm.payment_peroid" placeholder="收付账期">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in paymentPeroidOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计算基准日期：" prop="benchmark_type" >
            <el-select v-model="dataForm.benchmark_type" placeholder="计算基准日期">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in benchmarkTypeOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生效日：" prop="effective_date">
            <el-date-picker
              v-model="dataForm.effective_date"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="生效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="失效日：" prop="fail_date">
            <el-date-picker
              v-model="dataForm.fail_date"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="失效日"
              class="input_postion"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="账期类型：" prop="peroid_type">
            <el-select v-model="dataForm.peroid_type" placeholder="账期类型">
              <el-option label="全选" value=""></el-option>
              <el-option
                v-for="item in peroidTypeOption"
                :key="item.code"
                :label="item.label"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="周期天数：" prop="peroid_days" class="serarch-inner">
            <el-input v-model="dataForm.peroid_days" auto-complete="off" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="收付款日：" prop="payment_date" class="serarch-inner">
            <el-input v-model="dataForm.payment_date" auto-complete="off" placeholder="请输入"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <label>操作日志</label>
      <el-table
        ref="fitlerTable"
        :data="mockLogs"
        height="130px"
        tooltip-effect="dark"
        header-align="left"
        align="left"
        :size="size"
        style="width: 100%"
      >
        <el-table-column label="操作用户ID">
          <template slot-scope="scope">{{ scope.row.operator_id }}</template>
        </el-table-column>
        <el-table-column label="操作人">
          <template slot-scope="scope">{{ scope.row.operator }}</template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template slot-scope="scope">{{ scope.row.operator_time }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">{{ scope.row.operator_content }}</template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">{{ scope.row.memo }}</template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="detailDialogVisible = false">{{$t('action.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        peroid_name: "",
        payment_peroid: "",
        peroid_type: "",
        startTime: "",
        endTime: ""
      },
      paymentPeroidOption: [
        {
          code: "1",
          label: "付款账期"
        }
      ],
      peroidTypeOption: [
        {
          code: "1",
          label: "按周"
        },
        {
          code: "2",
          label: "按月"
        },
        {
          code: "3",
          label: "按季度"
        },
        {
          code: "4",
          label: "按年"
        },
        {
          code: "5",
          label: "半月结"
        },
        {
          code: "6",
          label: "收货后"
        }
      ],
      benchmarkTypeOption: [
        {
          code: "1",
          label: "收货日期"
        },
        {
          code: "2",
          label: "发货日期"
        }
      ],
      options: {},
      columns: [
        { prop: "peroid_name", label: "账期名称", minWidth: 100 },
        { prop: "payment_peroid", label: "收付账期", minWidth: 80 },
        { prop: "peroid_type", label: "周期类型", minWidth: 80 },
        { prop: "peroid_days", label: "周期天数", minWidth: 80 },
        { prop: "payment_date", label: "收付款日", minWidth: 80 },
        { prop: "is_fail", label: "是否失效", minWidth: 100 },
        { prop: "effective_date", label: "生效日期", minWidth: 100 },
        { prop: "fail_date", label: "失效日期", minWidth: 100 },
        { prop: "create_por", label: "创建人", minWidth: 100 },
        { prop: "create_time", label: "创建时间", minWidth: 100 },
        { prop: "edit_por", label: "编辑人", minWidth: 100 },
        { prop: "edit_time", label: "编辑时间", minWidth: 100 }
      ],
      mockPage: {
        total: "5",
        rows: [
          {
            id: "1",
            peroid_name: "风阿达",
            payment_peroid: "1",
            benchmark_type: "1",
            peroid_type: "发货后",
            peroid_days: "0",
            payment_date: "1",
            effective_date: "2019-11-29",
            fail_date: "2026-10-31",
            is_fail: "生效中",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "merchant",
            edit_time: "2019-12-02"
          },
          {
            id: "2",
            peroid_name: "风阿达",
            payment_peroid: "1",
            benchmark_type: "1",
            peroid_type: "发货后",
            peroid_days: "0",
            payment_date: "1",
            effective_date: "2019-11-29",
            fail_date: "2026-10-31",
            is_fail: "生效中",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "merchant",
            edit_time: "2019-12-02"
          },
          {
            id: "3",
            peroid_name: "风阿达",
            payment_peroid: "1",
            benchmark_type: "1",
            peroid_type: "发货后",
            peroid_days: "0",
            payment_date: "1",
            effective_date: "2019-11-29",
            fail_date: "2026-10-31",
            is_fail: "生效中",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "merchant",
            edit_time: "2019-12-02"
          },
          {
            id: "4",
            peroid_name: "风阿达",
            payment_peroid: "1",
            benchmark_type: "1",
            peroid_type: "发货后",
            peroid_days: "0",
            payment_date: "1",
            effective_date: "2019-11-29",
            fail_date: "2026-10-31",
            is_fail: "生效中",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "merchant",
            edit_time: "2019-12-02"
          },
          {
            id: "5",
            peroid_name: "风阿达",
            payment_peroid: "1",
            benchmark_type: "1",
            peroid_type: "发货后",
            peroid_days: "0",
            payment_date: "1",
            effective_date: "2019-11-29",
            fail_date: "2026-10-31",
            is_fail: "生效中",
            create_por: "merchant",
            create_time: "2019-11-29",
            edit_por: "merchant",
            edit_time: "2019-12-02"
          }
        ]
      },

      mockLogs: [
        {
          operator_id: "12343",
          operator: "merchant",
          operator_time: "2019-11-29 18:33:19",
          operator_content: "新增账期规则",
          memo: ""
        }
      ],

      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false,
      detailDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      // 新增编辑界面数据
      dataForm: {
        peroid_name: "",
        payment_peroid: "",
        benchmark_type: "",
        peroid_type: "",
        effective_date: "",
        fail_date: ""
      },
      dataFormRules: {
        peroid_name: [
          { required: true, message: "请输入账期名称", trigger: "blur" }
        ],
        payment_peroid: [
          { required: true, message: "请输入收付账期", trigger: "blur" }
        ],
        benchmark_type: [
          { required: true, message: "请输入计算基准日期", trigger: "blur" }
        ],
        effective_date: [
          { required: true, message: "请输入生效日", trigger: "blur" }
        ],
        fail_date: [
          { required: true, message: "请输入失效日", trigger: "blur" }
        ],
        peroid_type: [
          { required: true, message: "请输入账期类型", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPageMock(this.mockPage);
    },
    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
      }
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            var this_ = this;
            this_.$message({ message: "操作成功", type: "success" });
            this_.$refs["dataForm"].resetFields();
            this_.dialogVisible = false;
          });
        }
      });
    },
    // 显示新增界面
    handleAdd: function() {
      this.resetForm("dataForm");
      this.dialogVisible = true;
      this.operation = true;
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 批量删除
    handleDelete: function(data) {
      var this_ = this;
      this_.$message({ message: "操作成功", type: "success" });
    },
    // 显示详情
    handleDetail: function(params) {
      console.log(params);
      this.detailDialogVisible = true;
      this.dataForm = Object.assign({}, params.row);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },

  mounted() {
    this.initColumns();
  }
};
</script>

<style scoped lang="scss">
</style>
